package main

import (
	"fmt"
)

// 编写一个程序，使用冒泡排序的方法排序一个包含整数的切片

func bubblesort(s []int) (r []int) {
	l := len(s)
	r = make([]int, l)
	for i := 0; i < l; i++ {
		min := s[0]
		l_s := len(s)
		index := 0

		for j := 1; j < l_s; j++ {
			if s[j] < min {
				min = s[j]
				index = j
			}
		}
		r[i] = min
		s = rmItem(s, index)
	}
	return
}

func rmItem(s []int, ind int) (r []int) {
	r = append(s[0:ind], s[ind+1:]...)
	// r = make([]int, len(s)-1)
	// copy(r[:ind], s[:ind])
	// copy(r[ind:], s[ind+1:])
	return
}

func bubSort(s []int) {
	for pass := 1; pass < len(s); pass++ {
		for i := 0; i < len(s)-pass; i++ {
			if s[i] > s[i+1] { // 大值"冒泡"到底部
				s[i], s[i+1] = s[i+1], s[i]
			}
		}
	}
}

func main() {
	arr := []int{5, 3, 65, 6, 77, 3, 22}
	// fmt.Println(bubblesort(arr))
	fmt.Println("befort sort: ", arr)
	bubSort(arr)
	fmt.Println("after sort: ", arr)
}
